package com.cjq.service;

import com.cjq.mapper.StudentssmMapper;
import com.cjq.pojo.Studentssm;
import com.cjq.pojo.StudentssmExample;
import com.cjq.vo.QueryVO2;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

@Service
public class StudentService {
    @Resource
    private StudentssmMapper studentssmMapper;

    //多条件分页查询  返回的是分页之后的结果
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)   //对于查询操作的事务管理可以不用写
    public PageInfo<Studentssm> queryByPage(Integer pageNum, Integer pageSize, QueryVO2 vo){
        StudentssmExample example = new StudentssmExample();
        StudentssmExample.Criteria criteria = example.createCriteria();  //创建存放查询条件的容器
        //将QueryVo封装类里面有的值的属性 添加到容器里面  作为查询条件
        if(vo!=null){
            if (vo.getStuName()!=null && !"".equals(vo.getStuName().trim())){
                criteria.andStunameLike("%"+vo.getStuName().trim()+"%");
            }
            if (vo.getStuNo()!=null && !"".equals(vo.getStuNo().trim())){
                criteria.andStunoEqualTo(vo.getStuNo().trim());
            }
            if (vo.getSex()!=null && vo.getSex()!=-1){
                criteria.andSexEqualTo(vo.getSex());
            }
            if (vo.getPhone()!=null && !"".equals(vo.getPhone().trim())){
                criteria.andPhoneEqualTo(vo.getPhone().trim());
            }
            if (vo.getProfession()!=null && !"".equals(vo.getProfession().trim())){
                criteria.andProfessionLike("%"+vo.getProfession().trim()+"%");
            }
        }
        //分页
        PageHelper.startPage(pageNum,pageSize);     //这个分页只对紧邻的这条SQL语句有效
        List<Studentssm> studentssms = studentssmMapper.selectByExample(example);
        for (Studentssm studentssm : studentssms) {
            System.out.println(studentssm);
        }
        return new PageInfo<>(studentssms);
    }

    //添加学员
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})   //添加事务管理
    public int addStudent(Studentssm studentssm){
        return studentssmMapper.insertSelective(studentssm);
    }

    //删除学员
    //逻辑删除删除  根据主键逻辑删除球队，就是更新球队的Del属性变为1，并不是删除该球队信息
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})   //添加事务管理
    public int deleteStudent(int stuid){
        int i = studentssmMapper.deleteByPrimaryKey(stuid);
        return i;
    }

    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)   //对于查询操作的事务管理可以不用写
    public  Studentssm selectStudent(Integer stuid){
        return studentssmMapper.selectByPrimaryKey(stuid);
    }

    //更新球队
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})   //添加事务管理
    public int updateStudent(Studentssm studentssm){
        return studentssmMapper.updateByPrimaryKeySelective(studentssm);   //根据主键动态更新
    }


}
